package com.gmall.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicExchange_Queue_Config {

    public final static String TOPIC_EXCHANGE = "topic_exchange";
    // 声明队列
    public final static String TOPIC_EX_QUEUE_1 = "topic_queue_1";

    public final static String TOPIC_EX_QUEUE_2 = "topic_queue_2";



    // 声明交换机
    @Bean("topicExchange")
    public Exchange topicExchange(){
        //Builder 设计模式
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE)
                .durable(true)//默认RabbitMQ 重启以后， 交换机就删掉，除非是durable=true
                .build();
    }

    // 声明队列
    @Bean("topicQueue1")
    public Queue topicQueue1(){
        //return new Queue(DIRECT_EX_QUEUE_SMS,true);
        return QueueBuilder.durable(TOPIC_EX_QUEUE_1).build();
    }
    @Bean("topicQueue2")
    public Queue topicQueue2(){
        return QueueBuilder.durable(TOPIC_EX_QUEUE_2).build();
    }

    @Bean("topicBinding1")
    public Binding fanoutBinding1(@Qualifier("topicQueue1") Queue queue,
                                    @Qualifier("topicExchange") Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with("inform.#.email.#").noargs();
    }

    @Bean("topicBinding2")
    public Binding fanoutBinding2(@Qualifier("topicQueue2") Queue queue,
                                  @Qualifier("topicExchange") Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with("inform.#.sms.#").noargs();
    }


}
